/*
 *
 *  Copyright 2015-2019 the original author or authors.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *
 */

package com.zwl.swagger.plugins;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.MoreObjects;
import com.google.common.base.Optional;
import com.zwl.swagger.CommentContainer;
import com.zwl.swagger.comment.MethodComment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ValueConstants;
import springfox.documentation.service.ResolvedMethodParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.ParameterBuilderPlugin;
import springfox.documentation.spi.service.contexts.OperationContext;
import springfox.documentation.spi.service.contexts.ParameterContext;
import springfox.documentation.spring.web.DescriptionResolver;

import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import static com.google.common.base.Strings.emptyToNull;

/**
 *  星号结束或开头，会标记为必传
* @author zhangweilin
* @date 2021/10/27 22:02
* @Description:  必传优化
*/
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
@Slf4j
public class ParameterRequiredReaderPlus implements ParameterBuilderPlugin, ContextDescriper {
    private final DescriptionResolver descriptions;

    @Autowired
    CommentContainer commentContainer;

    @Autowired
    public ParameterRequiredReaderPlus(DescriptionResolver descriptions) {
        this.descriptions = descriptions;
    }

    @Override
    public void apply(ParameterContext context) {
        ResolvedMethodParameter methodParameter = context.resolvedMethodParameter();
        context.parameterBuilder()
                .required(isRequired(context.getOperationContext(), methodParameter));
    }

    @Override
    public boolean supports(DocumentationType delimiter) {
        return true;
    }

    private boolean isRequired(
            OperationContext operationContext,
            ResolvedMethodParameter methodParameter) {

        Set<Boolean> requiredSet = new HashSet<Boolean>();

        // when the type is Optional, the required property of @RequestParam/@RequestHeader doesn't matter,
        // since the value is always a non-null Optional after conversion
        boolean optional = isOptional(methodParameter);

        Optional<RequestParam> requestParam = methodParameter.findAnnotation(RequestParam.class);
        if (requestParam.isPresent()) {
            requiredSet.add(!optional && isRequired(requestParam.get()));
        }

        Optional<RequestHeader> requestHeader = methodParameter.findAnnotation(RequestHeader.class);
        if (requestHeader.isPresent()) {
            requiredSet.add(!optional && requestHeader.get().required());
        }

        Optional<PathVariable> pathVariable = methodParameter.findAnnotation(PathVariable.class);
        Optional<String> defaultParamName = methodParameter.defaultName();
        if (pathVariable.isPresent()) {
            String paramName = MoreObjects.firstNonNull(
                    emptyToNull(pathVariable.get().name()),
                    defaultParamName.orNull());

            if (pathVariable.get().required() ||
                    optionalButPresentInThePath(operationContext, pathVariable.get(), paramName)) {
                requiredSet.add(true);
            }
        }

        Optional<RequestBody> requestBody = methodParameter.findAnnotation(RequestBody.class);
        if (requestBody.isPresent()) {
            requiredSet.add(!optional && requestBody.get().required());
        }

        Optional<RequestPart> requestPart = methodParameter.findAnnotation(RequestPart.class);
        if (requestPart.isPresent()) {
            requiredSet.add(!optional && requestPart.get().required());
        }

        //增加对*的必传支持
        String description = "";
        try {
            description = getMethodDescription(operationContext);
            MethodComment methodComment = commentContainer.getMethodCommentMap().get(description);
            if (null != methodComment) {
                String comment = methodComment.getComment();
                Map<String, String> paramCommentMap = methodComment.getParamCommentMap();
                if (!CollectionUtils.isEmpty(paramCommentMap)) {
                    Set<Entry<String, String>> entrySet = paramCommentMap.entrySet();

                    int count = 0;
                    for (Entry<String, String> entry : entrySet) {
                        String key = entry.getKey();
                        key = key == null ? "":key.trim()  ;
                        String value = entry.getValue();
                        value = value == null ?"": value.trim() ;
                        count++;
                        //                    comment += "<div style=‘padding-left:0px;’><br>参数" + count + ": " + key + ": " + value+"</div>";
//                        comment += "<br>参数" + count + ": " + key + ": " + value;
                        if (defaultParamName.isPresent()) {
                            if (key.trim().equals(defaultParamName.get())) {
                                Boolean required = isRequired(value);
                                if (required) {
                                    requiredSet.add(required);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
			log.error("OperationNotesReaderPlus异常，description: " + description,e);
        }


        return requiredSet.contains(true);
    }

    private boolean optionalButPresentInThePath(
            OperationContext operationContext,
            PathVariable pathVariable,
            String paramName) {

        return !pathVariable.required()
                && operationContext.requestMappingPattern().contains("{" + paramName + "}");
    }

    @VisibleForTesting
    @SuppressWarnings("squid:S1872")
    boolean isOptional(ResolvedMethodParameter methodParameter) {
        return "java.util.Optional".equals(methodParameter.getParameterType().getErasedType().getName());
    }

    private boolean isRequired(RequestParam annotation) {
        String defaultValue = descriptions.resolve(annotation.defaultValue());
        boolean missingDefaultValue = ValueConstants.DEFAULT_NONE.equals(defaultValue);
        return annotation.required() && missingDefaultValue;
    }
}
